package com.chazz.dao.impl;

import com.chazz.dao.CfgoodsDao;
import com.chazz.entity.Goods;
import com.chazz.entity.GoodsSku;
import com.chazz.entity.Sku;
import com.chazz.utils.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CfgooodsDaoImpl implements CfgoodsDao {

    @Override
    public List<GoodsSku> findGoodsSkuBycategoryid(int categoryid) throws SQLException {
        List<GoodsSku> goodsSkuList=new ArrayList<>();
        List<Goods> goodsList=new ArrayList<>();
        String sql="SELECT * FROM goods WHERE categoryid=?";

        ResultSet rs= DBUtils.executeQuery(sql,categoryid);
//        int rowCount = 0;
//        while(rs.next()) {
//            rowCount++;
//        }
//        System.out.println("rowCout的值"+rowCount);
        while(rs.next()){
            System.out.println("sjkadghsaudh");
            Goods goods=new Goods();
            goods.setGoodsid(rs.getInt("goodsid"));
            goods.setGoodsno(rs.getString("goodsno"));
            goods.setGoodsname(rs.getString("goodsname"));
            goods.setGoodstitle(rs.getString("goodstitle"));
            goods.setGoodsimg(rs.getString("goodsimg"));
            goods.setCategoryid(rs.getInt("categoryid"));
            goods.setBrandid(rs.getInt("brandid"));
            goods.setGoodsdescription(rs.getString("goodsdescription"));
            goods.setStatus(rs.getInt("status"));
            goods.setCreatetime(rs.getDate("createtime"));
            goods.setUpdatetime(rs.getDate("updatetime"));
            goods.setRemark(rs.getString("remark"));
            goodsList.add(goods);

        }

         System.out.println( "第一个goodLsit"+goodsList.size());
        for(Goods goods1:goodsList) {
            Integer gid=goods1.getGoodsid();
            String sql1="SELECT * FROM sku WHERE goodsid=?";
            System.out.println(sql1);
            ResultSet resultSet=DBUtils.executeQuery(sql1,gid);

            while (resultSet.next()){
                Sku  sku=new Sku();
                sku.setSpecsid(resultSet.getInt("specsid"));
                sku.setGoodsid(resultSet.getInt("goodsid"));
                sku.setGoodsattribute(resultSet.getString("goodsattribute"));
                sku.setStock(resultSet.getInt("stock"));
                sku.setPrice(resultSet.getString("price"));
                sku.setStatus(resultSet.getInt("status"));
                sku.setCreatetime(resultSet.getDate("createtime"));
                sku.setUpdate(resultSet.getDate("update"));
                GoodsSku goodsSku=new GoodsSku();
                goodsSku.setGoods(goods1);
                goodsSku.setSku(sku);
                goodsSkuList.add(goodsSku);
            }

        }
        System.out.println("cfgoodsSkuList里的集合长度"+goodsSkuList.size());
        System.out.println("cfgoodslist里的集合长度"+goodsList.size());
        rs.close();
        DBUtils.close();
        return goodsSkuList;
    }
}
